package com.kfm.base.thread.day03;

import java.time.LocalDateTime;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Demo03 {

    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(1);

        Make t1 = new Make("厨子", blockingQueue);
        Eat t2 = new Eat("八戒", blockingQueue);
        Eat t3 = new Eat("文博", blockingQueue);
        Eat t4 = new Eat("秦爽", blockingQueue);

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        System.out.println("main end");
    }
}

class Make extends Thread{

    BlockingQueue<String> blockingQueue;

    public Make(String name, BlockingQueue<String> blockingQueue) {
        super(name);
        this.blockingQueue = blockingQueue;
    }

    public void run(){
        while (!isInterrupted()){
            try {
                sleep(3000);
                blockingQueue.put(LocalDateTime.now().toString());
                System.out.println("厨师放入一个鲜肉包");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class Eat extends Thread{

    BlockingQueue<String> blockingQueue;

    public Eat(String name, BlockingQueue<String> blockingQueue) {
        super(name);
        this.blockingQueue = blockingQueue;
    }

    public void run(){
        while (!isInterrupted()){
            try {
                sleep(1000);
                String take = blockingQueue.take();
                System.out.println(getName() + " 吃 " + take);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
